package com.fs.system.college_admin.service;

import com.fs.system.college_admin.mapper.ActiveCreditMapper;
import com.fs.system.exception.CustomerException;
import com.fs.system.student.domain.ActCredit;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class ActiveCreditService {

    @Resource
    private ActiveCreditMapper activeCreditMapper;

    public void addActiveCredit(ActCredit actCredit) {
        ActCredit dbActCredit = activeCreditMapper.selectActiveCreditByName(actCredit.getActName());
        if(Objects.nonNull(dbActCredit)){
            throw new CustomerException("500","新增失败！活动名称已存在");
        }
        actCredit.setDeleted(false);
        actCredit.setCreateTime(LocalDateTime.now());
        activeCreditMapper.addActiveCredit(actCredit);
    }

    public List<ActCredit> selectActiveCreditList(ActCredit actCredit) {
        return activeCreditMapper.selectActiveCreditList(actCredit);
    }

    public PageInfo<ActCredit> selectPage(ActCredit actCredit, Integer pageNum, Integer pageSize) {
        //开启分页查询（传参：当前页码、每页分页个数）
        PageHelper.startPage(pageNum,pageSize);
        //查询所有的数据
        List<ActCredit> activeCreditList = activeCreditMapper.selectActiveCreditList(actCredit);
        //PageInfo执行分页操作
        return PageInfo.of(activeCreditList);
    }

    public void updateActiveCredit(ActCredit actCredit) {
        Long actIdUpdate = actCredit.getActId();
        String actNameUpdate = actCredit.getActName();
        ActCredit dbActCredit = activeCreditMapper.selectActiveCreditById(actIdUpdate);
        if (!dbActCredit.getActName().equals(actNameUpdate)) {
            ActCredit dbActCredit02 = activeCreditMapper.selectActiveCreditByName(actNameUpdate);
            if (Objects.nonNull(dbActCredit02)) {
                throw new CustomerException("500", "更新失败！活动名称已存在");
            }
        }
        actCredit.setUpdateTime(LocalDateTime.now());
        activeCreditMapper.updateActiveCredit(actCredit);
        /**
         * 最开始写的逻辑
         */
        /*if(dbActCredit.getActName().equals(actCredit.getActName())){
            actCredit.setUpdateTime(LocalDateTime.now());
            activeCreditMapper.updateActiveCredit(actCredit);
        }else{
            ActCredit dbActCredit02 = activeCreditMapper.selectActiveCreditByName(actCredit.getActName());
            if(Objects.nonNull(dbActCredit02)){
                throw new CustomerException("500","更新失败！活动名称已存在");
            }
            actCredit.setUpdateTime(LocalDateTime.now());
            activeCreditMapper.updateActiveCredit(actCredit);
        }*/
    }

    /**
     * 单个删除
     * @param actId：需修改的id
     * @param updateBy：修改操作的人
     */
    public void deleteActiveCredit(Long actId, String updateBy) {
        ActCredit actCredit = activeCreditMapper.selectActiveCreditById(actId);
        if(Objects.isNull(actCredit)){
            throw new CustomerException("500","删除失败");
        }
        actCredit.setDeleted(true);
        actCredit.setUpdateBy(updateBy);
        actCredit.setUpdateTime(LocalDateTime.now());
        activeCreditMapper.updateActiveCredit(actCredit);
    }

    /**
     * 批量删除
     * @param updateBy
     * @param ids
     */
    public void deleteBatch(List<Long> ids,String updateBy) {
        for (Long actId :ids) {
            this.deleteActiveCredit(actId, updateBy);
        }
    }
}
